'use server'

import {getRefreshToken, resetAuthCookies} from "@/lib/actions";
import {getObj, patchObj, postObj} from "@/services/api_service";
import {redirect} from "next/navigation";
import {TypeVehicle} from "@/shared/types";

export async function logoutUser() {
    try {
        const refresh = await getRefreshToken();
        await postObj('/api/auth/logout/', {
            "refresh": refresh,
        });
        await resetAuthCookies();
    } catch (error) {
        console.error('Ошибка при выходе из системы:', error);
    } finally {
        redirect('/');
    }
}

export async function getFerryData(ferryId: string) {
    return await getObj(`/api/ships/ferries/${ferryId}/`)
}

export async function saveFerry(data: FormData) {
    return await postObj('/api/ships/ferries/', data);
}

export async function updateFerry(data: FormData, ferryId: string) {
    return await patchObj(`/api/ships/ferries/${ferryId}/`, data);
}

export async function getCaptainData(captainId: string) {
    return await getObj(`/api/ships/captains/${captainId}/`)
}

export async function saveCaptain(data: FormData) {
    return await postObj('/api/ships/captains/', data);
}

export async function updateCaptain(data: FormData, captainId: string) {
    return await patchObj(`/api/ships/captains/${captainId}/`, data);
}

export async function saveVehicle(data: TypeVehicle) {
    return await postObj('/api/ships/vehicles/', data);
}